package com.hyd.project;

import com.hyd.application.MyApplication;
import com.hyd.service.MusicService;
import com.hyd.view.RippleView;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 活动详情界面
 * 
 * @author llf
 * 
 */
public class ActionDetailActivity extends Activity {
	private AlertDialog dialog;
	private Window window;
	private Dialog dialog_share;
	private LinearLayout dialog_view;
	private LinearLayout ll_weibo, ll_weixinF, ll_weixinP, ll_qq, ll_qzone;
	private Button cancel;

	// 音乐播放相关
	private RelativeLayout player_view;
	private ProgressBar progress;
	private Handler handler;
	private Runnable runnable;
	private ImageView play, loop;
	private RippleView playRipple, loopRipple, nextRipple, priorRipple;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_action_detail);

		initView();
		initClick();
	}

	private void initView() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				ActionDetailActivity.this);
		builder.setTitle("联系商家");
		builder.setMessage("18589564652");
		builder.setPositiveButton("联系", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ "18268958649"));
				startActivity(intent);
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();

		// 第三方分享
		dialog_view = (LinearLayout) LayoutInflater.from(
				ActionDetailActivity.this).inflate(R.layout.dialog_view, null);
		cancel = (Button) dialog_view.findViewById(R.id.cancel);
		ll_weibo = (LinearLayout) dialog_view.findViewById(R.id.weibo);
		ll_weixinF = (LinearLayout) dialog_view.findViewById(R.id.wechat);
		ll_weixinP = (LinearLayout) dialog_view
				.findViewById(R.id.wechatmoments);
		ll_qq = (LinearLayout) dialog_view.findViewById(R.id.qq);
		ll_qzone = (LinearLayout) dialog_view.findViewById(R.id.qzone);
		dialog_share = new Dialog(ActionDetailActivity.this);
		dialog_share.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog_share.setContentView(dialog_view);

		// 播放器相关
		player_view = (RelativeLayout) findViewById(R.id.player_view);
		play = (ImageView) player_view.findViewById(R.id.play);
		loop = (ImageView) player_view.findViewById(R.id.loop);
		playRipple = (RippleView) player_view.findViewById(R.id.play_ripple);
		loopRipple = (RippleView) player_view.findViewById(R.id.loop_ripple);
		nextRipple = (RippleView) player_view.findViewById(R.id.next_ripple);
		priorRipple = (RippleView) player_view.findViewById(R.id.prior_ripple);
		progress = (ProgressBar) player_view.findViewById(R.id.seek);

		handler = new Handler();
		runnable = new Runnable() {

			@Override
			public void run() {
				if (MyApplication.playstatus == 1) {
					progress.setMax(MusicService.player.getDuration());
					progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				// 延迟启动线程，主要是为了保证启动线程的时候,播放音乐的服务已经启动
				handler.postDelayed(runnable, 50);
			}
		};

		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}

	private void initClick() {
		ll_weibo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActionDetailActivity.this, "分享到新浪微博",
						Toast.LENGTH_SHORT).show();
			}
		});

		ll_weixinF.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActionDetailActivity.this, "分享到微信好友",
						Toast.LENGTH_SHORT).show();
			}
		});

		ll_weixinP.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActionDetailActivity.this, "分享到微信盆友圈",
						Toast.LENGTH_SHORT).show();
			}
		});

		ll_qq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActionDetailActivity.this, "分享到qq好友",
						Toast.LENGTH_SHORT).show();
			}
		});

		ll_qzone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActionDetailActivity.this, "分享到qq空间",
						Toast.LENGTH_SHORT).show();
			}
		});

		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog_share.dismiss();
			}
		});

		playRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.playstatus == 0) {
					play.setBackgroundResource(R.drawable.player_hesitation);
					MyApplication.playstatus = 1;
					MusicService.goon();
					handler.post(runnable);
				} else {
					MusicService.pause();
					MyApplication.playstatus = 0;
					play.setBackgroundResource(R.drawable.play);

				}
			}
		});

		// 设置为循环播放
		loopRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.isLoop) {
					loop.setBackgroundResource(R.drawable.unloop);
					MusicService.player.setLooping(false);
					MyApplication.isLoop = false;
				} else {
					loop.setBackgroundResource(R.drawable.loop);
					MusicService.player.setLooping(true);
					MyApplication.isLoop = true;
				}
			}
		});

		nextRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playNext();
			}
		});

		priorRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playPrior();
			}
		});
	}

	// 分享活动
	public void share(View v) {
		window = dialog_share.getWindow();
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.mydialog);
		dialog_share.show();
		window.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams params = window.getAttributes();
		params.width = WindowManager.LayoutParams.MATCH_PARENT;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		window.setAttributes(params);
	}

	public void rush(View v) {
		Intent intent = new Intent(ActionDetailActivity.this,
				ActionPaymentActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
	}

	public void call(View v) {
		dialog.show();
	}

	public void back(View v) {
		finish();
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(runnable);
		super.onDestroy();
	}
}
